package com.nuanshui.heatedloan.baseservice.service;


import com.nuanshui.heatedloan.baseservice.dal.mapper.CommonConfigMapper;
import com.nuanshui.heatedloan.baseservice.facade.CommonConfigService;
import com.nuanshui.heatedloan.baseservice.form.CommonConfigForm;
import com.nuanshui.heatedloan.baseservice.vo.CommonConfigVO;
import com.nuanshui.heatedloan.constant.CacheConstants;
import com.nuanshui.heatedloan.constant.DubboCacheRegion;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.dubbo.extension.cache.manager.DubboCacheManager;
import com.nuanshui.heatedloan.pageable.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(ServiceConstant.COMMONCONFIGSERVICE)
@Transactional(readOnly=true,  value = TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
public class CommonConfigServiceImpl extends CommonServiceImpl implements CommonConfigService {

	@Autowired
	private CommonConfigMapper commonConfigMapper;

	@Autowired
	private DubboCacheManager dubboCacheManager;
	
	/**
	 * 根据id获取某个配置
	 * @param 
	 * @author guohao
	 * @date 2016年3月21日 下午2:27:12
	 * @since 2.6.0
	 */
	@Override
	public CommonConfigVO load(String id) {
		
		return commonConfigMapper.load(id);
	}

	@Override
	public Pager<CommonConfigVO> list(Pager<CommonConfigVO> pager, CommonConfigForm commonConfigForm) {
		List<CommonConfigVO> list = (List<CommonConfigVO>)dubboCacheManager.get(DubboCacheRegion.CACHE_COMMON_CONFI_LIST,commonConfigForm.getType());
		if (list == null){
			list =commonConfigMapper.list(pager,commonConfigForm);
			dubboCacheManager.put(DubboCacheRegion.CACHE_COMMON_CONFI_LIST,list,commonConfigForm.getType());
		}
		pager.setData(list);
		return pager;
	}

	@Override
	@Transactional(TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void save(CommonConfigVO form) {
		dubboCacheManager.evictCache(DubboCacheRegion.CACHE_COMMON_CONFI_LIST,form.getType());
		commonConfigMapper.save(form);
	}

	@Override
	@Transactional(TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void update(CommonConfigVO form) {
		dubboCacheManager.evictCache(DubboCacheRegion.CACHE_COMMON_CONFI_LIST,form.getType());
		commonConfigMapper.update(form);
	}

	@Override
	@Transactional(TransactionManagerType.BASE_SERVICE_TRANSACTION_MANAGER)
	public void delete(String id) {
		CommonConfigVO vo= this.load(id);
		if(vo != null ){
			dubboCacheManager.evictCache(DubboCacheRegion.CACHE_COMMON_CONFI_LIST,vo.getType());
		}
		commonConfigMapper.delete(id);
	}

	/**
	 * 根据配置的key获取一个配置对象
	 * @param 
	 * @author guohao
	 * @date 2016年3月21日 下午2:27:48
	 * @since 2.6.0
	 */
	@Override
	public CommonConfigVO loadByKey(String key) {
	
		return commonConfigMapper.loadByKey(key);
	}

	/**
	 * 根据配置的key获取所有的配置项
	 * @param 
	 * @author guohao
	 * @date 2016年3月21日 下午2:28:48
	 */
	@Override
	public List<CommonConfigVO> listByKey(String key) {
		
		return commonConfigMapper.listByKey(key);
	}

	/**
	 * 根据配置的key获取所有值
	 * @param 
	 * @author guohao
	 * @date 2016年3月21日 下午2:45:37
	 */
	@Override
	public List<String> listValueByKey(String key) {
	
		return commonConfigMapper.listValueByKey(key);
	}
	
	@Override
	@Cacheable(value= CacheConstants.COMMON_CONFIG_VALUE,key="#key")
	public String loadValueByKey(String key) {
		
		return commonConfigMapper.loadValueByKey(key);
	}
	
	
	@Override
	public String loadValueByNameAndKey(String name,String key){
		
		return commonConfigMapper.loadValueByNameAndKey(name,key);
		
	}

	@Override
	public List<String> listNameLikeValue(String value) {
	 
		return commonConfigMapper.listNameLikeValue(value);
	}

}
